package ahblue.cn.tanzi.ui.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.model.circle.Circle;
import ahblue.cn.tanzi.util.GlideApp;
import ahblue.cn.tanzi.view.decoration.SimpleItemDecoration;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用于显示圈子分类列表
 */
public class CircleShowAllListFragment extends Fragment {

    private static final String ARG_CATEGORY_ID = "ARG_CATEGORY_ID";

    @BindView(R.id.recyclerViewArticleList)
    RecyclerView mRecyclerViewArticleList;

    @BindView(R.id.wv_circle_introduce)
    WebView wvCircleIntroduce;

    private int mCircleCategoryId;
    private int mPage = 0;
    private final static int PAGE_COUNT = 10;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param categoryId 圈子分类ID.
     * @return A new instance of fragment CircleShowAllListFragment.
     */
    public static CircleShowAllListFragment newInstance(int categoryId) {
        CircleShowAllListFragment fragment = new CircleShowAllListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_CATEGORY_ID, categoryId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mCircleCategoryId = getArguments().getInt(ARG_CATEGORY_ID);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_article_list, container, false);
        ButterKnife.bind(this, view);
        wvCircleIntroduce.setVisibility(View.GONE);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initData();
    }

    private void initData() {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.getBigShotCategoryCircle(mPage, PAGE_COUNT, mCircleCategoryId,"", mCallbackCategoryCircle);
    }

    /**
     * 获取某个分类下文章的回调函数。
     */
    ApiCallback<List<Circle>> mCallbackCategoryCircle = new ApiCallback<List<Circle>>() {

        @Override
        public void onSuccess(List<Circle> data) {
            if (getActivity() == null || getActivity().isFinishing()) {
                return;
            }
            CirclesForCategoryAdapter adapter = new CirclesForCategoryAdapter(data);
            adapter.setOnItemChildClickListener(mCircleItemChildClickListener);
            mRecyclerViewArticleList.setLayoutManager(new LinearLayoutManager(getContext()));
            mRecyclerViewArticleList.setAdapter(adapter);
            mRecyclerViewArticleList.addItemDecoration(new SimpleItemDecoration(getContext()));
        }

        @Override
        public void onFailed(String errorMsg) {

        }
    };

    /**
     * 圈子列表的adapter
     */
    class CirclesForCategoryAdapter extends BaseQuickAdapter<Circle, BaseViewHolder> {

        CirclesForCategoryAdapter(@Nullable List<Circle> data) {
            super(R.layout.item_circle_values_list, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Circle item) {
            ImageView iv = helper.getView(R.id.img_icon);
            GlideApp.with(iv).load(item.head_portrait)
                    .placeholder(R.drawable.user_default_avatar)
                    .error(R.drawable.user_default_avatar)
                    .into(iv);

            helper.setText(R.id.tv_name, item.circle_name);
            helper.setText(R.id.tv_name, item.circle_name);
            helper.setText(R.id.tv_member_count, "成员数 " + Integer.toString(item.member_count));
            helper.setText(R.id.tv_post_count, "动态数 " + Integer.toString(item.dynamic_count));

            helper.addOnClickListener(R.id.btn_join);
        }
    }

    /**
     * 用户单击"加入"时的事件处理
     */
    BaseQuickAdapter.OnItemChildClickListener mCircleItemChildClickListener = new BaseQuickAdapter.OnItemChildClickListener() {
        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Circle circle = (Circle) adapter.getData().get(position);
            final Intent intent = new Intent(CircleShowAllListFragment.this.getContext(), CircleDetailActivity.class);
            intent.putExtra(CircleDetailActivity.ARG_CIRCLE_ID, circle.id);
            CircleShowAllListFragment.this.startActivity(intent);
        }
    };
}
